﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlueLotus7.SSQAnalysisCore;
using System.Xml.Linq;
using BlueLotus7.SSQAnalyzeWebApplication.Properties;
using BlueLotus7.SSQAnalyzeWebApplication.ViewModel;
using BlueLotus7.Common;

namespace BlueLotus7.SSQAnalyzeWebApplication.Controllers
{
    public class ConsoleController : AsyncController
    {
        //
        // GET: /Console/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Syncs lottery data.
        /// </summary>
        /// <param name="fullySync"></param>
        /// <returns></returns>
        public JsonResult SyncLotteryData(bool fullySync = false)
        {
            AjaxResult result = new AjaxResult();

            try
            {
                XDocument xDocument = XDocument.Load(Settings.Default.SyncLotteryUrl);
                SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
                service.SyncLotteryHistoryData(xDocument, fullySync);
            }
            catch (Exception ex)
            {
                Framework.Logger.LogException(new Exception("Failed to sync lottery data.", ex));
                result.ErrorMessage = "System error.";
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Imports lottery data
        /// </summary>
        /// <param name="xmlContent"></param>
        /// <param name="fullySync"></param>
        /// <returns></returns>
        public JsonResult ImportLotteryData(string xmlContent, bool fullySync = false)
        {
            AjaxResult result = new AjaxResult();

            try
            {
                XDocument xDocument = XDocument.Parse(xmlContent);
                SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
                service.SyncLotteryHistoryData(xDocument, fullySync);
            }
            catch (Exception ex)
            {
                Framework.Logger.LogException(new Exception("Failed to import lottery data.", ex));
                result.ErrorMessage = "System error.";
            }

            return Json(result);
        }
    }
}
